package com.example.my;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class login extends AppCompatActivity implements View.OnClickListener {
    private Button bt_login, bt_register;
    private EditText et_ID, et_password;
    private String username, password;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private SQLiteDatabase db;
    private static String TABLE_NAME = "students";
    private CheckBox chkBoxMainPSW;
    private CheckBox chkBoxMainName;
    private EditText etID;
    private EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        et_ID = (EditText) findViewById(R.id.et_ID);
        et_password = (EditText) findViewById(R.id.et_password);
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_register = (Button) findViewById(R.id.bt_register);
        bt_register.setOnClickListener(this);
        bt_login.setOnClickListener(this);
        sp = getSharedPreferences("user", MODE_PRIVATE);
        editor = sp.edit();
        initView();
        sp = getSharedPreferences("login", MODE_PRIVATE);
        chkBoxMainName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean b) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("chkName", b);
                editor.commit();
            }
        });
        chkBoxMainPSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean b) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("chkPSW", b);
                editor.commit();
                if (b) {
                    chkBoxMainName.setChecked(true);
                    chkBoxMainPSW.setEnabled(false);
                } else {
                    chkBoxMainName.setEnabled(true);
                }
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            /*
            判断账号密码，有一个为空则吐司"账号或密码不能为空！"
            账号密码都为1则登录成功，否则失败
             */
            case R.id.bt_login:
                username = et_ID.getText().toString().trim();
                password = et_password.getText().toString().trim();
                if (et_ID.getText().toString().trim().equals("") ||
                        et_password.getText().toString().trim().equals("")) {
                    Toast.makeText(this, "账号或密码不能为空！", Toast.LENGTH_SHORT).show();
                } else {
                    userService userService = new userService(this);
                    Boolean flag = userService.login(username, password);
                    if (flag == Boolean.FALSE) {
                        Toast.makeText(this, "账号或密码不正确！", Toast.LENGTH_SHORT).show();

                        return;
                    } else {
                        if (chkBoxMainName.isChecked()) {
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("userName", username);
                            editor.commit();
                        }
                        if (chkBoxMainPSW.isChecked()) {
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("password", password);
                            editor.commit();
                        }
                        editor.putString("username", username);
                        editor.commit();
                        Intent intent = new Intent(login.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
                break;

            case R.id.bt_register:
                Intent intent = new Intent(this, register.class);
                startActivity(intent);
                break;
        }
    }

    protected void onStart() {
        super.onStart();
        boolean chkName = sp.getBoolean("chkName", false);
        chkBoxMainName.setChecked(chkName);
        boolean chkPSW = sp.getBoolean("chkPSW", false);
        chkBoxMainPSW.setChecked(chkPSW);
        if (chkName) {
            etID.setText(sp.getString("userName", null));
        }
        if (chkPSW) {
            etPassword.setText(sp.getString("password", null));
        }
    }

    private void initView() {
        chkBoxMainPSW = (CheckBox) findViewById(R.id.chkBoxMainPSW);
        chkBoxMainName = (CheckBox) findViewById(R.id.chkBoxMainName);
        etID = (EditText) findViewById(R.id.et_ID);
        etPassword = (EditText) findViewById(R.id.et_password);
    }
}
